//
//  ColorPickerViewController.m
//  iOSStudy
//
//  Created by Evan Xie on 11/30/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import "ColorPickerViewController.h"
#import "RSColorPickerView.h"
#import "RSBrightnessSlider.h"


@interface ColorPickerViewController () <RSColorPickerViewDelegate>
{
    RSColorPickerView *_colorPicker;
    RSBrightnessSlider *_brightnessSlider;
    UIView *_colorPatch;
    
    UILabel *_RGBLabel;
    UILabel *_HSVLabel;
}
@end

@implementation ColorPickerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat hPadding = 20.0;
    CGFloat width = self.view.frame.size.width;
    
    _colorPicker = [[RSColorPickerView alloc] initWithFrame:CGRectMake(hPadding, 80, width-2*hPadding, width-2*hPadding)];
    [_colorPicker setDelegate:self];
    [_colorPicker setBrightness:1.0];
    [_colorPicker setCropToCircle:YES]; // Defaults to YES (and you can set BG color)
    [_colorPicker setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:_colorPicker];
    
    _brightnessSlider = [[RSBrightnessSlider alloc] initWithFrame:CGRectMake(hPadding, CGRectGetMaxY(_colorPicker.frame) + 20.0, width-2*hPadding, 30.0)];
    [_brightnessSlider setColorPicker:_colorPicker];
    [_brightnessSlider setUseCustomSlider:YES]; // Defaults to NO
    [self.view addSubview:_brightnessSlider];
    
    _colorPatch = [[UIView alloc] initWithFrame:CGRectMake(hPadding, CGRectGetMaxY(_brightnessSlider.frame) + 10.0, width-2*hPadding, 30.0)];
    [self.view addSubview:_colorPatch];
    
    CGFloat yOffset = CGRectGetMaxY(_colorPatch.frame) + 20.0;
    CGRect labelFrame = _colorPatch.frame;
    labelFrame.origin.y = yOffset;
    
    _RGBLabel = [[UILabel alloc] initWithFrame:labelFrame];
    _RGBLabel.font = [UIFont fontWithName:@"AvenirNextCondensed-HeavyItalic" size:20.0];
    _RGBLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_RGBLabel];
    
    labelFrame.origin.y = CGRectGetMaxY(_RGBLabel.frame) + 10.0;
    _HSVLabel = [[UILabel alloc] initWithFrame:labelFrame];
    _HSVLabel.font = [UIFont fontWithName:@"AvenirNextCondensed-HeavyItalic" size:20.0];
    _HSVLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_HSVLabel];
}

#pragma mark - RSColorPickerViewDelegate

- (void)colorPickerDidChangeSelection:(RSColorPickerView *)cp
{
    UIColor *color = [cp selectionColor];
    _colorPatch.backgroundColor = color;
    _RGBLabel.textColor = color;
    _HSVLabel.textColor = color;
    
    float red, green, blue, hue, satuation, brightness;
    [color getRed:&red green:&green blue:&blue alpha:NULL];
    [color getHue:&hue saturation:&satuation brightness:&brightness alpha:NULL];
    
    _RGBLabel.text = [NSString stringWithFormat:@"RGB (%li, %li, %li)", (long)(255*red), (long)(255*green), (long)(255*blue)];
    _HSVLabel.text = [NSString stringWithFormat:@"HSV (%li, %li, %li)", (long)(360*hue), (long)(100*satuation), (long)(100*brightness)];
}

@end
